package com.jshare.service.Impl;

import java.util.List;

import javax.annotation.Resource;


import org.springframework.stereotype.Service;

import com.jshare.dao.NewsDAO;
import com.jshare.entity.Member;
import com.jshare.entity.News;
import com.jshare.service.NewsService;
import com.jshare.util.Pager;

@Service("newsService")
public class NewsServiceImpl implements NewsService {

	@Resource(name="newsDao")
	private NewsDAO newsDao;
	public void addAnews(News news) {
		// TODO Auto-generated method stub
		newsDao.save(news);
	}
	public List getAllTitle() {
		
		return newsDao.findAllTitle();
	}
	public List listAllNews() {
		// TODO Auto-generated method stub
		return newsDao.findAll();
	}

	public NewsDAO getNewsDao() {
		return newsDao;
	}

	public void setNewsDao(NewsDAO newsDao) {
		this.newsDao = newsDao;
	}
	@Override
	public boolean isExist(int id) {
		// TODO Auto-generated method stub
		News news = newsDao.findById(id);
		if(news == null)
		{
			return false;
		}
		return true;
	}
	@Override
	public void deleteAnews(int id) {
		// TODO Auto-generated method stub
		newsDao.delete(newsDao.findById(id));	
	}
	@Override
	public Pager getPageData(int pageSize, int pageNow) {
		// TODO Auto-generated method stub
		String sql = "from News";
		int rowCount = newsDao.getTotalRows("select count(*) from News as news");
		
		if(pageSize <= 0 )
		{
			pageSize=5;
		}
		Pager pager = new Pager(pageSize,pageNow,rowCount);
		pager.setData(newsDao.getPage(sql,(pager.getPagenow()-1)*pager.getPageSize(),pager.getPageSize()));
		return pager;
	}
	@Override
	public News getNewsById(int id) {
		// TODO Auto-generated method stub
		return newsDao.findById(id);
	}
	

}
